What is causing this behavior with the movement of Pong Ball in 2D? [closed]
Posted
by
thegermanpole
on Game Development
See other posts from Game Development
or by thegermanpole
Published on 2012-03-23T17:49:12Z
Indexed on
2012/03/23
23:40 UTC
Read the original article
Hit count: 265
//edit after running it through the debugger it turned out i had the display function set to x,x...TIL how to use a debugger
I've been trying to teach myself C++ SDL with the lazyfoo tutorial and seem to have run into a roadblock. The code below is the movement function of my Dot class, which controls the ball. The ball seems to ignore yvel and moves with xvel to the bottom right. The code should be pretty readable, the rest of the relevant facts are:
- All variables are names
- Constants are in caps
dotradis the radius of my dotyvelandxvelare set to 5 in the constructor- The dot is created at
xandyequal to 100 When I comment out the x movement block it doesn't move, but if i comment out the y movement block, it keeps on going down to the right.
void Dot::move() { if(((y+yvel+dotrad) <= SCREEN_HEIGHT) && (0 <= (y-dotrad+yvel))) { y+=yvel; } else { yvel = -1*yvel; } if(((x+xvel+dotrad) <= SCREEN_WIDTH) && (0 <= (x-dotrad+xvel))) { x +=xvel; } else { xvel = -1*xvel; } }
© Game Development or respective owner